#include "AbsElement.h"
#include "QFontMetrics"
AbsElement::AbsElement(QObject *prent):QObject(prent)

{

    m_nameStr.clear();
    m_attributeMap.clear();
    m_styleSheet.clear();
    m_attributeMap["style"] = "";
}

AbsElement::AbsElement(const AbsElement &absEle, QObject *prent) :QObject(prent),
    ElementStytle(absEle)
{

    m_nameStr = absEle.nameStr();
    m_attributeMap = absEle.attributeMap();
    m_styleSheet =absEle.styleSheet();
}

QString AbsElement::toString(){
    QString html = QString("<%1  %2 > %3 </%4>").arg(nameStr()).arg(forMateAttr())
            .arg(context()).arg(nameStr());
    return html;
}

void AbsElement::forMate()
{
    clear();

    initMate();
}


QString AbsElement::nameStr() const
{
    return m_nameStr;
}

void AbsElement::setNameStr(const QString &nameStr)
{
    m_nameStr = nameStr;
}

void AbsElement::setAttrbute(QString key, QString value)
{
    m_attributeMap[key] = '"'+value+'"';
}

void AbsElement::appendElement(AbsElement *ele)
{
    Q_UNUSED(ele);
}

void AbsElement::clear()
{
    m_nameStr.clear();
    m_attributeMap.clear();
    m_attributeMap["style"] = "";
}

void AbsElement::forMateStyleMap(QString &styleMap)
{
    QStringList strList = styleMap.split(";");
    foreach ( QString str, strList) {
        QString temp =str;
        if(temp.replace(" ","")!="")
        {
            QStringList itemMap = str.split(":");
            m_styleSheet[itemMap[0].simplified()] = itemMap[1].simplified();
        }
    }
}

QString AbsElement::forMateStyle()
{
    QString fontStyle = this->italic()?"italic ":"";
    fontStyle += this->bold()?"bold ":"";
    fontStyle += tr(" %1px  ").arg(this->pixelSize());
    fontStyle += this->family();

    fontStyle =  QString("%1:%2;%3:%4; ").arg("color").arg(QColor::name())\
            .arg("font").arg(fontStyle);
    forMateStyleMap(fontStyle);

    QString styleSheet;
    styleSheet.clear();
    QStringList keys = m_styleSheet.keys();
    foreach (const QString &key, keys) {
        styleSheet+= (key+":"+m_styleSheet.value(key)+"; ");
    }
    return "\""+ styleSheet+"\"";
}

QString AbsElement::forMateAttr()
{
    m_attributeMap["style"] =  forMateStyle();
    QStringList keys = m_attributeMap.keys();
    QString mateAttr;
    foreach ( QString key, keys) {
        mateAttr += QString(" %1 = %2").arg(key).arg(m_attributeMap.value(key));
    }
    return mateAttr;
}

QMap<QString, QString> AbsElement::styleSheet() const
{
    return m_styleSheet;
}

void AbsElement::setStyleSheet(const QMap<QString, QString> &styleSheet)
{
    m_styleSheet = styleSheet;
}

void AbsElement::setStyleSheet(const QString key, const QString value)
{
    m_styleSheet[key] = value;
}

void AbsElement::setEleWidth(int width)
{
    m_styleSheet["width"] = QString::number(width)+"px";
}

void AbsElement::setEleHeight(int heigth)
{
    m_styleSheet["height"] = QString::number(heigth)+"px";
}



QMap<QString, QString> AbsElement::attributeMap() const
{
    return m_attributeMap;
}




